/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carpark.servicies;

import cz.muni.fi.pa165.carpark.TO.RoleTO;
import cz.muni.fi.pa165.carpark.TO.UserTO;
import cz.muni.fi.pa165.carpark.dao.UserDAO;
import cz.muni.fi.pa165.carpark.entities.Role;
import cz.muni.fi.pa165.carpark.entities.User;
import java.util.LinkedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.ConversionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Martin
 */
@Service
public class AuthorizationServiceImpl implements AuthorizationService {

    @Autowired
    @Qualifier("TOconverter")
    private ConversionService convertor;
    @Autowired
    private UserDAO userDAO;

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException {
        return this.userDAO.loadUserByUsername(string);
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    public void setConvertor(ConversionService convertor) {
        this.convertor = convertor;
    }

    @Override
    @Transactional
    public UserTO retrieve(String username) throws UsernameNotFoundException {
        User user = (User) this.loadUserByUsername(username);
        UserTO u = convertor.convert(user, UserTO.class);
        u.setRoles(new LinkedList<RoleTO>());
        for (Role r : user.getRoles()) {
            u.getRoles().add(convertor.convert(r, RoleTO.class));
        }
        return u;
    }

    @Override
    @PreAuthorize("hasRole('superadmin')")
    @Transactional
    public void create(UserTO u) {
        User user = convertor.convert(u, User.class);
        user.setRoles(new LinkedList<Role>());
        for (RoleTO r : u.getRoles()) {
            user.getRoles().add(convertor.convert(r, Role.class));
        }
        userDAO.create(user);
    }

    @Override
    @Transactional
    public void createSuper(UserTO u) {
        if (!u.getPassword().equals("admin")) {
            throw new IllegalArgumentException("Superadmin should have password admin");
        }
        if (!u.getUserName().equals("admin")) {
            throw new IllegalArgumentException("Superadmin should have username admin");
        }
        User user = convertor.convert(u, User.class);
        user.setRoles(new LinkedList<Role>());
        for (RoleTO r : u.getRoles()) {
            user.getRoles().add(convertor.convert(r, Role.class));
        }
        userDAO.create(user);
    }
}
